<?php

class CpordersController extends Core_Controller_Action 
{
	private $_orders;
	
	public function init() {
		$this->_orders = new Orders();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cporders' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$item = $this->_orders->getItem( $id )->getFirst();

		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();
			
			$save = false;
			
			if( $post['state'] !=  $item['state'] ) {
	
				$mail = new Core_Mail();
				
				if( $post['state'] > 0 ) {
				    
				    if($item['vouchers_v_id']>0)
				    {
				        // to jest bon
        				$attachment = $mail->createAttachment( file_get_contents( getcwd() . '/public/faktury/bon_' . $id . '.pdf' ) );
        				$attachment->filename = 'bon_' . $id . '.pdf';
				    }
				    else
				    {
				        // to sa produkty
        				$attachment = $mail->createAttachment( file_get_contents( getcwd() . '/public/faktury/faktura_' . $id . '_vat.pdf' ) );
        				$attachment->filename = 'faktura_' . $id . '_vat.pdf';
				    }
				}
				
				$mail->addTo( $item['email'], $item['email'] );
				$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
				$mail->setSubject( $this->view->translate( 'ch_state_topic_' . $post['state'] ) );
				$mail->setBodyHtml( 
					sprintf( 
						$this->view->translate( 'ch_state_body_' . $post['state'] ),
						$item['o_id']
					) 
				);
				$mail->send();
				
				$item['state'] = $post['state'];
				
				$save = true;
			}
			
			if( $post['state2'] !=  $item['state2'] ) {
	
				$mail = new Core_Mail();
				$mail->addTo( $item['email'], $item['email'] );
				$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
				$mail->setSubject( $this->view->translate( 'ch_state2_topic_' . $post['state2'] ) );
				$mail->setBodyHtml( 
					sprintf( 
						$this->view->translate( 'ch_state2_body_' . $post['state2'] ),
						$item['o_id']
					) 
				);
				$mail->send();
				
				$item['state2'] = $post['state2'];
				
				$save = true;
			}
			
			if( $post['state3'] !=  $item['state3'] ) {
				$mail = new Core_Mail();
				$mail->addTo( $item['email'], $item['email'] );
				$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
				$mail->setSubject( $this->view->translate( 'ch_state3_topic_' . $post['state3'] ) );
				$mail->setBodyHtml( 
					sprintf( 
						$this->view->translate( 'ch_state3_body_' . $post['state3'] ),
						$item['o_id']
					) 
				);
				$mail->send();
				
				$item['state3'] = $post['state3'];
				
				$save = true;
			}
			
			if( $post['state4'] !=  $item['state4'] ) {
	
				$mail = new Core_Mail();
				$mail->addTo( $item['email'], $item['email'] );
				$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
				$mail->setSubject( $this->view->translate( 'ch_state4_topic_' . $post['state4'] ) );
				$mail->setBodyHtml( 
					sprintf( 
						$this->view->translate( 'ch_state4_body_' . $post['state4'] ),
						$item['o_id']
					) 
				);
				$mail->send();
				
				$item['state4'] = $post['state4'];
				
				$save = true;
			}
			
			if( $post['state5'] !=  $item['state5'] ) {
	
				$mail = new Core_Mail();
				$mail->addTo( $item['email'], $item['email'] );
				$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
				$mail->setSubject( $this->view->translate( 'ch_state5_topic_' . $post['state5'] ) );
				$mail->setBodyHtml( 
					sprintf( 
						$this->view->translate( 'ch_state5_body_' . $post['state5'] ),
						$item['o_id']
					) 
				);
				$mail->send();
				
				$item['state5'] = $post['state5'];
				
				$save = true;
			}
			
			if( $save ) {
				$item->save();
			}
		}
		
		$this->view->item = $item;
		
		$this->assignType( 0 );
	}
	
	public function listAction() {
		$page = intval( $this->_getParam( 'page', 1 ) );
		$this->view->state = $this->_getParam( 'state', 2 );
		$this->view->state2 = $this->_getParam( 'state2', 2 );
		$this->view->state3 = $this->_getParam( 'state3', 2 );
		$this->view->state4 = $this->_getParam( 'state4', 2 );
		$this->view->state5 = $this->_getParam( 'state5', 2 );
		$this->view->province = $this->_getParam( 'province', 16 );
		$this->view->o_id = $this->_getParam( 'o_id', 0 );
		$this->view->from = $this->_getParam( 'from', false );
		$this->view->to = $this->_getParam( 'to', false );
		
		$excel = $this->_getParam( 'excel', 0 );
		
		$limit = 12;
		if( $excel ) {
			$limit = 10000;
		}
		
		$pagerLayout = $this->_orders->getListWithPager( $page, $this->view->baseUrl(), array(
			'state' => $this->view->state,
			'state2' => $this->view->state2,
			'state3' => $this->view->state3,
			'state4' => $this->view->state4,
			'state5' => $this->view->state5,
			'province' => $this->view->province,
			'o_id' => $this->view->o_id,
			'from' => $this->view->from,
			'to' => $this->view->to
		), $limit );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		$this->view->arrayprovince =  Core_Static::$province;
		$this->view->arraycountry =  Core_Static::$country;

        
		if( $excel ) {
		    error_reporting(0);
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			
			
			echo 'Numer;Kwota;Zapłacone;Potwierdzone;Przygotowne;Wysłane;Data_dodania;Email;Wysylka_imie;Wysylka_nazwisko;Wysylka_ulica;Wysylka_nr_domu;Wysylka_nr_mieszkania;Wysylka_miasto;Wysylka_kod_pocztowy;Wysylka_wojewodztwo;Wysylka_kraj;Faktura_nip;Faktura_nazwa_firmy;Faktura_imie;Faktura_nazwisko;Faktura_ulica;Faktura_nr_domu;Faktura_nr_mieszkania;Faktura_miasto;Faktura_kod_pocztowy;Faktura_wojewodztwo;Faktura_kraj' . "\n";
			
			foreach( $this->view->list as $item ) {	
				$state = ( $item['state'] > 0 ) ? 'Tak' : 'Nie';
				$state2 = ( $item['state2'] > 0 ) ? 'Tak' : 'Nie';
				$state3 = ( $item['state3'] > 0 ) ? 'Tak' : 'Nie';
				$state4 = ( $item['state4'] > 0 ) ? 'Tak' : 'Nie';

				echo $item['o_id'] . ';' . $item['price'] . ' ' . $item['currency'] . ';' . $state  . ';' . $state2 . ';' . $state3 . ';' . $state4 . ';' . $item['add_date'] .';'.$item['email'].';'.$item['name_to_dispatch'].';'.$item['surname_to_dispatch'].';'.$item['street_to_dispatch'].';'.$item['nr_home_to_dispatch'].';'.$item['nr_flat_to_dispatch'].';'.$item['locality_to_dispatch'].';'.$item['zip_to_dispatch'].';'.$this->view->arrayprovince[$item['province_to_dispatch']].';'.$this->view->arraycountry[$item['country_to_dispatch']].';'. $item['nip_to_fv'].';'.$item['company_to_fv'].';'.$item['name_to_fv'].';'.$item['surname_to_fv'].';'.$item['street_to_fv'].';'.$item['nr_home_to_fv'].';'.$item['nr_flat_to_fv'].';'.$item['locality_to_fv'].';'.$item['zip_to_fv'].';'.$this->view->arrayprovince[$item['province_to_fv']].';'.$this->view->arraycountry[$item['country_to_fv']]. "\n";
			}

			$this->getResponse()->setHeader( 'Content-type', 'application/txt' );
			$this->getResponse()->setHeader( 'Content-Disposition', 'attachment; filename="orders.csv"' );
		}

		$this->assignType( 0 );
	}
	
	public function findAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$q = $this->_getParam( 'q' );

		$item = $this->_orders->suggest( $q );
		if( count( $item ) ) {
			$result = array();
			foreach ( $item->toArray() as $value ) {
				//Core_Debug::pr( $value );
				$result[] = array(
					'id' => $value['o_id'],
					'to' => $value['name_to_dispatch'] . ' ' . $value['surname_to_dispatch']
				);
			}
			
			echo Zend_Json::encode( $result );
		}
	}

	public function invitationAction() {
	
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Zamówienia';
				break;
		}
	}
}